// pages/result_query/result_query.ts
export { };
const urls = require("../../utils/urls");
const my_date = require("../../utils/my_date");
const tc_cos = require('../../utils/tc_cos');


class TaskInfo {
    static cout: number = 0;
    "uuid": string;
    "bottle_id": string;
    "video_url": string;
    "upload_time": string;
    "video_state": string = "0";
    "check_result_state": string = "0";
    "user_uuid": string;
    // value show on view
    "upload_time_show": string;
    "video_state_show": string = "0";
    "check_result_state_show": string = "0";
    [propName: string]: any;
    // sort key
    "k": number;
    constructor(d: TaskInfo) {
        TaskInfo.cout += 1;
        // let attr: ;
        for (let attr of ["uuid", "bottle_id", "video_url", "upload_time", "video_state", "check_result_state", "user_uuid"]) {
            this[attr] = d[attr];
        }
        this.k = TaskInfo.cout;
    };
    static video_state_map: any = {
        "-1": "审核未通过", "0": "审核中", "1": "审核通过"
    };
    static check_result_state_map: any = {
        "0": "检测中", "1": "检测完成"
    };

    get_show_data() {
        TaskInfo.cout += 1;
        this.k = TaskInfo.cout;
        this.upload_time_show = my_date.get_readable_date(this.upload_time);
        this.video_state_show = TaskInfo.video_state_map[this.video_state];
        this.check_result_state_show = TaskInfo.check_result_state_map[this.check_result_state];
    }
}


Page({

    /**
     * 页面的初始数据
     */
    data: {
        "task_list": ([] as TaskInfo[]),
        "app_user_info": ({} as any),
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.get_task_list();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    get_task_list() {
        var that = this;
        const app_user_info = wx.getStorageSync("app_user_info");
        const user_uuid = app_user_info.uuid;
        wx.request({
            url: urls.add_check_task_by_user_uuid_url,
            data: {
                user_uuid: user_uuid
            },
            dataType: 'json',
            success: function (res: any) {
                console.log("get_task_list success");
                console.log(res);
                let tmp: TaskInfo[] = res.data.data;
                let tasks = [];
                for (let i = 0; i < tmp.length; i += 1) {
                    let task = new TaskInfo(tmp[i]);
                    task.get_show_data();
                    tasks.push(task);
                }
                that.setData({
                    task_list: tasks,
                })
            }
        })
    },
    download_check_result_file() {
        const file_remote_path = "check_report/12151545check-result.pdf";

        // get object
        let get_object_callback = (err: any, data: any) => {
            console.log("get_object_callback");
            console.log(err);
            console.log(data);
            if (err != null) {
                wx.showToast({
                    title: '下载失败',
                    icon: 'error',
                    duration: 2000,
                    mask: true
                })
            } else {
                // 打开文件
                wx.downloadFile({
                    url: data.Url, // 需要加 url 的域名作为下载白名单
                    success(res) {
                        console.log(res.statusCode, res.tempFilePath);
                        wx.openDocument({
                            filePath: res.tempFilePath,
                            showMenu: true,
                        });
                    },
                    fail: function (err) {
                        console.log(err);
                    },
                });
            }
        }
        tc_cos.getObjectUrl(file_remote_path, get_object_callback);
    }
})